//
//  CGSchoolDetailOnceViewController.m
//  SchoolClient
//
//  Created by mac on 15/12/11.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGSchoolDetailOnceViewController.h"

@interface CGSchoolDetailOnceViewController ()

@property(nonatomic,assign)CGFloat fontSize;

@end

@implementation CGSchoolDetailOnceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"校招详情";
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.fontSize = 13;
    //发送请求获取公司产品详情
    [self fetchInfoWithProduct_id:self.school_id];
    [self layoutUI];
}

-(void)layoutUI{
    //容器View
    CGRect bFrame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 120);
    UIView *containView = [[UIView alloc]initWithFrame:bFrame];
    containView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:containView];
    
    //招聘标题Label
    CGRect cFrame = CGRectMake(5, 0, SCREEN_WIDTH - 30, 44);
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:cFrame];
    titleLabel.text = self.mySchoolInfo.title;
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.font = [UIFont systemFontOfSize:self.fontSize + 3];
    titleLabel.numberOfLines = 2;
    [containView addSubview:titleLabel];
    
    //公司名称Label
    cFrame.origin.y = CGRectGetMaxY(titleLabel.frame);
    cFrame.size.height = 20;
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:cFrame];
    nameLabel.text = self.mySchoolInfo.name;
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.font = [UIFont systemFontOfSize:self.fontSize + 1];
    [containView addSubview:nameLabel];
    
    //招聘时间Label
    cFrame.origin.y = CGRectGetMaxY(nameLabel.frame);
    cFrame.size.width = 70;
    UILabel *item1 = [[UILabel alloc]initWithFrame:cFrame];
    item1.text = @"招聘时间:";
    item1.textColor = [UIColor grayColor];
    item1.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item1];
    
    cFrame.origin.x += 70;
    cFrame.size.width = SCREEN_WIDTH - 30 - 70;
    UILabel *content1 = [[UILabel alloc]initWithFrame:cFrame];
    //content1.text = @"2015-10-26 16:07";
    content1.text = [self getTimeWithDate:self.mySchoolInfo.recruitmentTimeStart];
    content1.textColor = [UIColor grayColor];
    content1.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content1];
    
    //工作地点Label
    cFrame = item1.frame;
    cFrame.origin.y = CGRectGetMaxY(item1.frame);;
    UILabel *item2 = [[UILabel alloc]initWithFrame:cFrame];
    item2.text = @"招聘地点:";
    item2.textColor = [UIColor grayColor];
    item2.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item2];
    
    cFrame.origin.x += 70;
    cFrame.size.width = SCREEN_WIDTH - 30 - 70;
    UILabel *content2 = [[UILabel alloc]initWithFrame:cFrame];
    content2.text = self.mySchoolInfo.recruitmentArea;
    content2.textColor = [UIColor grayColor];
    content2.font = [UIFont systemFontOfSize:self.fontSize];
    //content2.backgroundColor = [UIColor redColor];
    [containView addSubview:content2];
    
    //分隔线
    cFrame = item2.frame;
    cFrame.origin.y += 25;
    cFrame.size.height = 1;
    cFrame.size.width = titleLabel.frame.size.width;
    UIView *lineView2 = [[UIView alloc]initWithFrame:cFrame];
    lineView2.backgroundColor = [Tools getColor:@"ebebeb"];
    [containView addSubview:lineView2];
    
    //职位描述webView
    bFrame.origin.y += 110;
    bFrame.size.height = SCREEN_HEIGHT - 64 - 130;
    UIWebView *webView = [[UIWebView alloc]initWithFrame:bFrame];
    
    NSString *myContent = [self.mySchoolInfo.content stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    myContent = [NSString stringWithFormat:@"<html><body>%@</body></html>",myContent];
    
    [webView loadHTMLString:myContent baseURL:nil];
    [self.view addSubview:webView];
    UIScrollView *scollview=(UIScrollView *)[[webView subviews]objectAtIndex:0];
    scollview.bounces=NO;
}

-(NSString *)getTimeWithDate:(NSString *)timeStr{
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc]init];
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    [inputFormatter setLocale:locale];
    [inputFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss aa"];
    NSDate *inputDate = [inputFormatter dateFromString:timeStr];
    //创建日期模板对象,设置模板的显示格式
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
    outputFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    NSString *outputDate = [outputFormatter stringFromDate:inputDate];     //自动转换成当地时间
    return outputDate;
}

#pragma mark 发送请求获取学校产品详情
-(void)fetchInfoWithProduct_id:(NSString *)product_id{
    
    //发送请求获取公司产品信息
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"id"] = product_id;
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/school/showInfo.app" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        //NSLog(@"result:%@",result);
        
        //去除前面字符串
        if ([result rangeOfString:@"true"].location != NSNotFound) {
            
            [self trimInfo:result WinthNumber:7];
            
        }else if ([result rangeOfString:@"false"].location != NSNotFound) {
        
            [self trimInfo:result WinthNumber:8];
            
        }else{
            
        [Tools alertWithTitle:@"温馨提示" andMessage:@"系统繁忙,请稍后再试"];
            
        }
        
 
    } failure:^(NSError *error) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        
    }];
    
}

#pragma mark 去除前面无关紧要的字符
-(void)trimInfo:(NSString *)product WinthNumber:(int)number{

    NSMutableString *mstr = [NSMutableString stringWithString:product];
    NSRange range = NSMakeRange(0, number);
    [mstr deleteCharactersInRange:range];
    //json转对象
   // NSLog(@"school:%@",mstr);
    NSData *valueData = [mstr dataUsingEncoding:NSUTF8StringEncoding];
    self.mySchoolInfo = [CGInfoSchool objectWithJSONData:valueData];
    [self layoutUI];
}


@end
